package com.tdam2013.grupo04.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tdam2013.grupo04.R;
import com.tdam2013.grupo04.modelo.Contacto;

public class Adapter extends BaseAdapter{
	private Context mContext;
	ArrayList<Contacto> array;
	//private LayoutInflater inflater;

	protected class Holder {

		private ImageView img;
		private TextView txtNombre;
		private TextView txtTelefono;
		private ImageView img1;
		private ImageView img2;
		private ImageView img3;
		private ImageView img4;
	}

	public Adapter(Context con, ArrayList<Contacto> mArray) {
		this.mContext = con;
		//this.inflater = LayoutInflater.from(con);
		array=mArray;
	}

	public int getCount() {
		return array.size() ;
	}

	public Object getItem(int posicion) {
		return array.get(posicion);
	}

	public long getItemId(int position) {
		return 0;
	}

public View getView(int position, View convertView, ViewGroup arg2) {
	Holder holder=null;
	Log.v("test","entra al adapter");
	if (convertView == null) {
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(R.layout.contact_item, null);
		holder = new Holder();
		holder.img = (ImageView) convertView.findViewById(R.id.iconContact);
		holder.txtNombre = (TextView) convertView.findViewById(R.id.txtContacName);
		holder.txtTelefono = (TextView) convertView.findViewById(R.id.txtPhoneContact);
		holder.img1 = (ImageView) convertView.findViewById(R.id.contacto_llamada);
		holder.img2 = (ImageView) convertView.findViewById(R.id.contacto_mail);
		holder.img3 = (ImageView) convertView.findViewById(R.id.contacto_mensaje);
		holder.img4 = (ImageView) convertView.findViewById(R.id.contacto_servicioweb);

		convertView.setTag(holder);

	} else {
		holder = (Holder) convertView.getTag();
	}
	
	Contacto contactoSeleccionado = (Contacto) this.getItem(position);
	
	//if(Fragmento1.estado=="activado" || Fragmento2.estado=="activado" )
	//{
	this.loadContactEmails(contactoSeleccionado);
	//}
//	if(Fragmento3.estado=="activado")
//	{
	this.loadContactTelephoneNumbers(contactoSeleccionado);
//	}
	if (contactoSeleccionado.getContactImageUri() == null)
	{
		holder.img.setImageResource(R.drawable.contacto);
	}
		else
	{
		holder.img.setImageURI(contactoSeleccionado.getContactImageUri());	
	}
	
//	if (holder.img.getDrawable() == null) {
//		holder.img.setImageResource(R.drawable.usuario_sin_foto);
//	}
	holder.txtNombre.setText(contactoSeleccionado.getNombre());
	holder.txtTelefono.setText(contactoSeleccionado.getNroTelefono(0));
	if (array.get(position).getMails().size() == 0) {
		holder.img1.setImageDrawable(null);
	} else {
		holder.img1.setImageResource(R.drawable.con_email);
	}
	if (array.get(position).getNombreUsuario() == null) {
		holder.img2.setImageDrawable(null);
	} else {
		holder.img2.setImageResource(R.drawable.con_usuario_chat);
		}

	if (array.get(position).getNroTelefonos().size() == 0) {
		holder.img3.setImageDrawable(null);
		holder.img4.setImageDrawable(null);
	} else {
		holder.img3.setImageResource(R.drawable.con_telefono);
		holder.img4.setImageResource(R.drawable.con_sms);
	}
	
	
	
	return convertView;
}
private void loadContactEmails(Contacto contact) {
	Cursor emailCur = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
			ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { String.valueOf(contact.getIdProvider()) }, null);
	while (emailCur.moveToNext()) {
		String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
		String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
		contact.addEmail(email, emailType);
	}
	emailCur.close();
}
private void loadContactTelephoneNumbers(Contacto contact) {
	Cursor pCur = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
			ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { String.valueOf(contact.getIdProvider()) }, null);
	while (pCur.moveToNext()) {
		contact.addNroTelefono(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
	}
	pCur.close();
}

}
